<template>
	<view class="intelligent-control">
		<view class="control-nav flex-center-around" >
			<view
				:class="navActived === item.id ? 'nav nav-actived' : 'nav'"
				v-for="(item, index) in navList"
				:key="item.id"
				@click="toggleNav(item.id)">
				{{item.name}}
			</view>
		</view>
		<checkbox-group v-if="deviceList.length" class="control-list" @change="checkboxChange">
			<label
				class="control-list-item"
				v-for="(item,index) in deviceList"
				:key="index">
				<view class="flex-center">
					<image
						class="control-list-item-img" 
						:src="item.product.image ? item.product.image : defaultImage" 
						mode="aspectFill"
					></image>
					<view class="control-list-item-right">
						<view class="name text-overflow-one">{{item.product.name || '新一代立升净饮一体机'}}</view>
						<view class="type">序列号:{{item.baseInfo.number}}</view>
						<view class="daterange" v-if="item.product.use_time">时间:{{item.product.use_time}}</view>
						<view class="address text-overflow-two">
							<!-- 地址:{{item.baseInfo.address ? item.baseInfo.address.split('-')[2]:''}} -->
						</view>
					</view>
					<switch class="control-list-item-switch" disabled :checked="item.switchChecked" />
					<checkbox class="control-list-item-checkbox" :value="item.product.id" :checked="item.checked" />
				</view>
			</label>
		</checkbox-group>
		
		<view v-if="!deviceList.length" class="no-data">
			<image class="no-data-img" src="../../static/images/no-data.png" mode="aspectFit"></image>
			暂无设备数据
		</view>
		<view class="btns flex-center-between" v-if="deviceList.length">
			<checkbox-group class="check-all" >
				<!-- <label @change="checkboxAllChange">
					<checkbox value="1" :checked="checkAll" />全选
				</label> -->
			</checkbox-group>
			<view class="btn" @click="control()">
				{{navActived === 0 ? '开机' : navActived === 1 ? '关机' : '清洗滤芯'}}
			</view>
		</view>
	</view>
	
</template>

<script>
	export default {
		components: {
			
		},
		data() {
			return {
				defaultImage: '/static/images/equipment.jpg',
				navActived: 1,
				navList: [
					{
						name: '开机',
						id: 1,
					},
					{
						name: '关机',
						id: 0,
					},
					{
						name: '清洗滤芯',
						id: 2,
					}
				],
				deviceList: [],
				checkAll: false,
				checkedDevice: null,
			}
		},
		onLoad() {
			this.getDeviceList()
		},
		methods: {
			/**
			 * 获取设备列表数据
			 */
			async getDeviceList() {
				let res = await this.$http.request({
					method: 'post',
					url: '/api/equipment/my-equipment',
					data: {
						status: this.navActived,
						state_and: JSON.stringify([["is_online","=","1"]])
					}
				})
				if (res.status === 1) {
					this.deviceList = res.data.list.map(item => {
						item.product.image = item.product.image ? this.$baseUrl + item.product.image: item.product.image;
						item.switchChecked = item.baseInfo.result_device.is_boot
						item.checked = false
						return item
					})
				} else {
					uni.showToast({
						title: res.message,
						icon: 'none',
						duration: 3000
					})
				}
			},
			/**
			 * 切换导航栏
			 * @param {Number} id
			 */
			toggleNav(id) {
				this.navActived = id
				this.getDeviceList();
			},
			
			checkboxChange(e) {
				let checkedArr = e.detail.value;
				if(checkedArr.length > 1){
					checkedArr.shift()
				}
				this.deviceList = this.deviceList.map(item => {
					if(checkedArr.map(Number).includes(Number(item.product.id))){
						item.checked = true
						this.checkedDevice = item.baseInfo.device_id;
					} else {
						item.checked = false
					}
					return item
				})
				this.checkAll = this.deviceList.every(item => {
					return item.checked === true
				})
				if(this.checkAll) {
					this.deviceList = this.deviceList.map(item => {
						item.checked = this.checkAll
						return item
					})
				}
			},
			checkboxAllChange(e) {
				this.checkAll = e.detail.value.length > 0 ? true : false
				this.deviceList = this.deviceList.map(item => {
					item.checked = this.checkAll
					return item
				})
			},
			async control() {
				let res = await this.$http.request({
					method: 'post',
					url: '/api/equipment/control',
					data: {
						equipment_id: this.checkedDevice,
						control: 0
					}
				})
				if (res.code == 1) {
					this.getDeviceList()
					
					// this.deviceList = res.data.list.map(item => {
					// 	item.product.image = this.$baseUrl + item.product.image
					// 	item.switchChecked = item.baseInfo.equipment_status
					// 	item.checked = false
					// 	return item
					// })
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 3000
					})
				}
				// uni.showModal({
				// 	title: '提示',
				// 	content: '智能控制功能尚未上线，敬请期待！',
				// 	showCancel: false
				// })
			},
			/**
			 * 跳转
			 * @param {String} page
			 */
			pageTo(page) {
				uni.navigateTo({
					url: page
				});
			},
			/**
			 * 返回
			 */
			pageBack() {
				uni.navigateBack();
			}
			
		}
	}
</script>

<style lang="scss">
	.intelligent-control{
		// padding-top: 110rpx;
		.control-nav{
			position: fixed;
			top: 0;
			left: 0;
			right: 0;
			z-index: 9;
			height: 90rpx;
			line-height: 90rpx;
			background: $uni-bg-color;
			border-bottom: 1rpx solid $uni-border-color;
			.nav{
				width: 150rpx;
				text-align: center;
			}
			.nav-actived{
				color: $uni-color-theme;
				border-bottom: 2rpx solid $uni-color-theme;
				animation: toggleNav 0.3s ease;
			}
		}
		.control-list{
			margin-top: 100rpx;
			margin-bottom: 100rpx;
			&-item{
				display: block;
				padding: 30rpx 40rpx;
				margin-bottom: 20rpx;
				background: $uni-bg-color;
				position: relative;
				&-img{
					height: 180rpx;
					width: 180rpx;
				}
				&-right{
					width: 450rpx;
					margin-left: 20rpx;
					.name{
						width: 400rpx;
					}
					.type,
					.daterange,
					.address{
						width: 420rpx;
						line-height: 1.5;
						font-size: $uni-font-size-sm2;
						color: $uni-text-color-grey;
					}
				}
				&-switch{
					position: absolute;
					top: 30rpx;
					right: 20rpx;
				}
				// &-checkbox{
				// 	position: absolute;
				// 	bottom: 30rpx;
				// 	right: 20rpx;
				// }
			}
		}
		
		.btns{
			position: fixed;
			bottom: 0;
			left: 0;
			right: 0;
			height: 106rpx;
			width: 100%;
			background: $uni-bg-color;
			.check-all{
				margin-left: 30rpx;
			}
			.btn{
				width: 238rpx;
				line-height: 106rpx;
				text-align: center;
				color: $uni-text-color-inverse;
				background: $uni-color-theme;
			}
		}
		
		@keyframes toggleNav {
			0% {
				transform: translateY(6rpx);
			}
			100% {
				transform: translateY(0%);
			}
		}
	}
</style>
